package com.dms.web.controller.system;

import java.io.IOException;
import java.util.List;

import com.dms.common.config.RuoYiConfig;
import com.dms.common.core.domain.entity.SysUser;
import com.dms.common.utils.file.FileUploadUtils;
import com.dms.system.domain.SysMissionPlant;
import com.dms.system.domain.SysMissionUser;
import com.dms.system.service.ISysPlantService;
import com.dms.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.dms.common.annotation.Log;
import com.dms.common.enums.BusinessType;
import com.dms.system.domain.SysMission;
import com.dms.system.service.ISysMissionService;
import com.dms.common.core.controller.BaseController;
import com.dms.common.core.domain.AjaxResult;
import com.dms.common.utils.poi.ExcelUtil;
import com.dms.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 任务Controller
 * 
 * @author xlong_zhu
 * @date 2021-04-01
 */
@Controller
@RequestMapping("/system/mission")
public class SysMissionController extends BaseController
{
    private static String prefix = "system/mission";
    private static final int ONCE=1;
    private static final int DAILY=2;
    private static final int WEEKLY=3;
    private static final int MONTHLY=4;

    @Autowired
    private ISysMissionService sysMissionService;

    @Autowired
    private ISysPlantService sysPlantService;

    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("system:mission:view")
    @GetMapping()
    public String mission()
    {
        return prefix + "/mission";
    }

    /**
     * 查询任务列表
     */
    @RequiresPermissions("system:mission:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMission sysMission)
    {
        startPage();
        List<SysMission> list = sysMissionService.selectSysMissionList(sysMission);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @RequiresPermissions("system:mission:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMission sysMission)
    {
        List<SysMission> list = sysMissionService.selectSysMissionList(sysMission);
        ExcelUtil<SysMission> util = new ExcelUtil<SysMission>(SysMission.class);
        return util.exportExcel(list, "mission");
    }

    /**
     * 新增任务
     * 这里的200表示设备部门的id号
     * 巡检人员只能选择设备部门员工
     */
    @GetMapping("/add")
    public String add(@RequestParam(defaultValue = "200") Long deptId, Model m)
    {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        m.addAttribute("userList",sysUserService.selectUserList(sysUser));
        m.addAttribute("plantList",sysPlantService.selectSysPlantList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("system:mission:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMission sysMission, SysMissionUser sysMissionUser, SysMissionPlant sysMissionPlant)
    {
        return toAjax(sysMissionService.insertSysMission(sysMission,sysMissionUser,sysMissionPlant));
    }

    /**
     * 修改任务
     * 查询任务关联的员工编号列表
     * 查询员工列表
     * 生成map
     */
    @GetMapping("/edit/{missionId}")
    public String edit(@RequestParam(defaultValue = "200") Long deptId,@PathVariable("missionId") Long missionId, ModelMap mmap)
    {
        SysMission sysMission = sysMissionService.selectSysMissionById(missionId);
        List<Long> userIds = sysMissionService.selectUserIdsByMissionId(missionId);
        List<Long> plantIds = sysMissionService.selectPlantIdsByMissionId(missionId);
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        List<SysUser> userList = sysUserService.selectUserList(sysUser);
        mmap.put("sysMission", sysMission);
        mmap.put("userIds",userIds);
        mmap.put("userList",userList);
        mmap.put("plantIds",plantIds);
        mmap.put("plantList",sysPlantService.selectSysPlantList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("system:mission:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMission sysMission,SysMissionUser sysMissionUser,SysMissionPlant sysMissionPlant)
    {
        setNull(sysMission);
        return toAjax(sysMissionService.updateSysMission(sysMission,sysMissionUser,sysMissionPlant));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("system:mission:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMissionService.deleteSysMissionByIds(ids));
    }

    /**
     * 讲封装好的sysMission对象中不必要的参数置null
     * 当cycle=1时，表示任务仅执行一次，则将missionWeekday，missionMonthday置null
     * 当cycle=2时，表示任务每日执行，则将missionDate，missionWeekday,missionMonthday置null
     * 当cycle=3时，表示任务每周执行，则将missionDate，missionMonthday置null
     * 当cycle=4时，表示任务每月执行，则将missionDate，missionWeekday置null
     * */
    public static void setNull(SysMission sysMission){
        switch (sysMission.getMissionCycle()){
            case ONCE: {
                sysMission.setMissionWeekday(null);
                sysMission.setMissionMonthday(null);
                break;
            }
            case DAILY:{
                sysMission.setMissionDate(null);
                sysMission.setMissionWeekday(null);
                sysMission.setMissionMonthday(null);
                break;
            }
            case WEEKLY:{
                sysMission.setMissionDate(null);
                sysMission.setMissionMonthday(null);
                break;
            }
            case MONTHLY:{
                sysMission.setMissionDate(null);
                sysMission.setMissionWeekday(null);
                break;
            }
            default:break;
        }
    }

    /**
     * 上传现场图片
     * */
    @PostMapping("/uploadMissionImg")
    @ResponseBody
    public String uploadMissionImg(MultipartFile file) throws IOException {
        if (!file.isEmpty())
        {
            return FileUploadUtils.upload(RuoYiConfig.getMissionImgPath(), file);
        }
        return "空文件";
    }
}